package com.google.coffeeshop.gui.desktop.receiptnote;

import java.util.ArrayList;
import java.util.List;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import com.google.coffeeshop.business.AvailabilityBusiness;
import com.google.coffeeshop.entity.ReceiptNoteLineDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class ReceiptNoteLineTable extends AbstractBaseTable {
	private static final long serialVersionUID = 1L;
	
	private static String COL_RNL_ID = "Receipt Note Line ID";
	private static String COL_PRODUCT_ID = "Product ID";
	private static String COL_PRODUCT = "Product";
	private static String COL_QUANTITY = "Quantity";
	private static String COL_UNIT_PRICE = "Unit Price";
	private static String COL_AMOUNT = "Amount";
	
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_RNL_ID, COL_PRODUCT_ID, COL_PRODUCT, COL_QUANTITY, COL_UNIT_PRICE, COL_AMOUNT
		};
	
	private DefaultTableModel model = new DefaultTableModel(
			rowData,
			columnNames
		);
	
	public ReceiptNoteLineTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
		invisibleColumn(0);
	}

	public void removeAllRow() {
		model.setRowCount(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	public void addNewRow(Long receiptNoteLineId, Long productId, String productName, Long quantity, Long unitPrice, Long amount) {
		Object[] objects = new Object[] {receiptNoteLineId, productId, productName, quantity, unitPrice, amount};
		model.addRow(objects);
	}

	public void addNewRow(ReceiptNoteLineDTO receiptNoteLine) {
		addNewRow(
				receiptNoteLine.getId(),
				receiptNoteLine.getProduct().getId(),
				receiptNoteLine.getProduct().getName(),
				receiptNoteLine.getQuantity(),
				receiptNoteLine.getPriceOfUnit(),
				receiptNoteLine.getQuantity() * receiptNoteLine.getPriceOfUnit()
		);
	}
	
	public void addNewRows(List<ReceiptNoteLineDTO> receiptNoteLines) {
		for (ReceiptNoteLineDTO receiptNoteLine : receiptNoteLines) {
			addNewRow(receiptNoteLine);
		}
	}
	
	public ReceiptNoteLineDTO getReceiptNoteLineSelected() {
		int rowIndex = this.getSelectedRow();
		ReceiptNoteLineDTO receiptNoteLine = new ReceiptNoteLineDTO();
		receiptNoteLine.setId(
				(long)findModelValue(model, COL_RNL_ID, rowIndex));
		receiptNoteLine.setProduct(
				AvailabilityBusiness.findProductById(
						(long)findModelValue(model, COL_PRODUCT_ID, rowIndex)));
		receiptNoteLine.setQuantity(
				(long)findModelValue(model, COL_QUANTITY, rowIndex));
		receiptNoteLine.setPriceOfUnit(
				(long)findModelValue(model, COL_UNIT_PRICE, rowIndex));
		return receiptNoteLine;
	}
	
	public List<ReceiptNoteLineDTO> getReceiptNoteLineList() {
		List<ReceiptNoteLineDTO> receiptNoteLines = new ArrayList<ReceiptNoteLineDTO>(); 
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			
			ReceiptNoteLineDTO receiptNoteLine = new ReceiptNoteLineDTO();
			receiptNoteLine.setId(
					(long)findModelValue(model, COL_RNL_ID, rowIndex));
			receiptNoteLine.setProduct(
					AvailabilityBusiness.findProductById(
							(long)findModelValue(model, COL_PRODUCT_ID, rowIndex)));
			receiptNoteLine.setQuantity(
					(long)findModelValue(model, COL_QUANTITY, rowIndex));
			receiptNoteLine.setPriceOfUnit(
					(long)findModelValue(model, COL_UNIT_PRICE, rowIndex));
					
			receiptNoteLines.add(receiptNoteLine);
		}
		return receiptNoteLines;
	}
	
}
